package com.unism.common.rest.errorhandling;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

import com.unism.common.rest.exception.ParamException;
import com.unism.common.rest.filters.AppConstants;

public class ParamExceptionMapper implements ExceptionMapper<ParamException> {

	public Response toResponse(ParamException ex) {
		ErrorMessage errorMessage = new ErrorMessage();		
		setHttpStatus(ex, errorMessage);
		errorMessage.setStatus(AppConstants.GENERIC_APP_ERROR_CODE);
		errorMessage.setMessage(ex.getMessage());
		errorMessage.setDeveloperMessage(ex.getStackTraceText());
		errorMessage.setLink(AppConstants.BLOG_POST_URL);
		return Response.status(500)
				.entity(errorMessage)
				.type(MediaType.APPLICATION_JSON).
				build();
	} 
	private void setHttpStatus(Throwable ex, ErrorMessage errorMessage) {
		if(ex instanceof ParamException ) { 
			errorMessage.setCode(((ParamException) ex).getErrNo()); 
		} 
	}
}
